"""
Case Type   : GUC_ErrorLog
Case Name   : 验证参数opfusion_debug_mode的几种设置方式
Create At   : 2022/06/07
Owner       : opentestcase019
Description :
    1.查看参数默认值，创建用户
    2.管理员用户设置数据库级别的参数
    3.管理员用户设置用户级别的参数
    4.管理员用户设置会话级别的参数
    5.普通用户设置用户级别的参数
    6.普通用户设置会话级别的参数
    7.通过gs_guc reload的方式设置参数
    8.通过gs_guc set的方式设置参数
    9.删除用户，恢复参数
Expect      :
    1.返回默认值off，创建用户成功
    2.设置成功，返回值log
    3.设置成功，返回值off
    4.设置成功，返回值log
    5.设置成功，返回值off
    6.设置成功，返回值log
    7.设置成功，返回值off
    8.设置成功，返回值log
    9.删除用户成功，恢复成功
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant


class ErrorLog(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()
        self.user_sysadmin = 'u_guc_errorlog_case0123_01'
        self.user_common = 'u_guc_errorlog_case0123_02'
        self.debug_mode = 'opfusion_debug_mode'
        self.debug_mode_default = self.common.show_param(self.debug_mode)
        self.show_cmd = f"show {self.debug_mode};"

    def test_error_log(self):
        text = '-----step1:查看参数默认值，创建用户;' \
               'expect:返回默认值off，创建用户成功-----'
        self.logger.info(text)
        show_msg = self.common.show_param(self.debug_mode)
        self.assertEquals(show_msg, 'off', '执行失败' + text)
        create_cmd = f"drop user if exists {self.user_sysadmin}," \
            f"{self.user_common};" \
            f"create user {self.user_sysadmin} sysadmin " \
            f"password '{macro.COMMON_PASSWD}';" \
            f"create user {self.user_common} password '{macro.COMMON_PASSWD}';"
        self.logger.info(create_cmd)
        create_msg = self.primary_sh.execut_db_sql(create_cmd)
        self.logger.info(create_msg)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG,
                      create_msg, '执行失败:' + text)
        self.assertEquals(2, create_msg.count(
            self.constant.CREATE_ROLE_SUCCESS_MSG), '执行失败:' + text)
        #
        text = '-----step2:管理员用户设置数据库级别的参数;expect:设置成功，返回值log-----'
        self.logger.info(text)
        sql_cmd = f"alter database postgres set {self.debug_mode} to log;"
        self.logger.info(sql_cmd)
        sql_msg = self.primary_sh.execut_db_sql(
            sql_cmd, f'-U {self.user_sysadmin} -W {macro.COMMON_PASSWD}')
        self.logger.info(sql_msg)
        self.assertIn(self.constant.ALTER_DATABASE_SUCCESS_MSG,
                      sql_msg, '执行失败' + text)
        show_msg = self.primary_sh.execut_db_sql(
            self.show_cmd, f'-U {self.user_sysadmin} -W {macro.COMMON_PASSWD}',
            'postgres')
        self.logger.info(show_msg)
        self.assertEquals('log', show_msg.splitlines()[-2].strip(),
                          '执行失败' + text)

        text = '-----step3:管理员用户设置用户级别的参数;expect:设置成功，返回值off-----'
        self.logger.info(text)
        sql_cmd = f"alter user {self.user_sysadmin} " \
            f"set {self.debug_mode} to off;"
        self.logger.info(sql_cmd)
        sql_msg = self.primary_sh.execut_db_sql(
            sql_cmd, f'-U {self.user_sysadmin} -W {macro.COMMON_PASSWD}')
        self.logger.info(sql_msg)
        self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG,
                      sql_msg, '执行失败' + text)
        show_msg = self.primary_sh.execut_db_sql(
            self.show_cmd, f'-U {self.user_sysadmin} -W {macro.COMMON_PASSWD}')
        self.logger.info(show_msg)
        self.assertEquals('off', show_msg.splitlines()[-2].strip(),
                          '执行失败' + text)

        text = '-----step4:管理员用户设置会话级别的参数;expect:设置成功，返回值log-----'
        self.logger.info(text)
        sql_cmd = f"set {self.debug_mode} to log;" \
            f"show {self.debug_mode};"
        self.logger.info(sql_cmd)
        sql_msg = self.primary_sh.execut_db_sql(
            sql_cmd, f'-U {self.user_sysadmin} -W {macro.COMMON_PASSWD}')
        self.logger.info(sql_msg)
        self.assertIn(self.constant.SET_SUCCESS_MSG,
                      sql_msg.splitlines()[0].strip(), '执行失败' + text)
        self.assertEquals('log', sql_msg.splitlines()[-2].strip(),
                          '执行失败:' + text)

        text = '-----step5:普通用户设置用户级别的参数;expect:设置成功，返回值off-----'
        self.logger.info(text)
        sql_cmd = f"alter user {self.user_common} " \
            f"set {self.debug_mode} to off;"
        self.logger.info(sql_cmd)
        sql_msg = self.primary_sh.execut_db_sql(
            sql_cmd, f'-U {self.user_common} -W {macro.COMMON_PASSWD}')
        self.logger.info(sql_msg)
        self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG,
                      sql_msg, '执行失败' + text)
        show_msg = self.primary_sh.execut_db_sql(
            self.show_cmd, f'-U {self.user_common} -W {macro.COMMON_PASSWD}')
        self.logger.info(show_msg)
        self.assertEquals('off', show_msg.splitlines()[-2].strip(),
                          '执行失败' + text)

        text = '-----step6:普通用户设置会话级别的参数;expect:设置成功，返回值log-----'
        self.logger.info(text)
        sql_cmd = f"set {self.debug_mode} to log;" \
            f"show {self.debug_mode};"
        self.logger.info(sql_cmd)
        sql_msg = self.primary_sh.execut_db_sql(
            sql_cmd, f'-U {self.user_common} -W {macro.COMMON_PASSWD}')
        self.logger.info(sql_msg)
        self.assertIn(self.constant.SET_SUCCESS_MSG,
                      sql_msg.splitlines()[0].strip(), '执行失败' + text)
        self.assertEquals('log', sql_msg.splitlines()[-2].strip(),
                          '执行失败:' + text)

        text = '-----step7:通过gs_guc reload的方式设置参数;expect:设置成功，返回值off-----'
        self.logger.info(text)
        guc_res = self.primary_sh.execute_gsguc('reload',
                                                self.constant.GSGUC_SUCCESS_MSG,
                                                f'{self.debug_mode}=off')
        self.assertTrue(guc_res, '执行失败' + text)
        show_msg = self.common.show_param(self.debug_mode)
        self.assertEquals(show_msg, 'off', '执行失败' + text)

        text = '-----step8:通过gs_guc set的方式设置参数;expect:设置成功，返回值log-----'
        self.logger.info(text)
        guc_res = self.primary_sh.execute_gsguc('set',
                                                self.constant.GSGUC_SUCCESS_MSG,
                                                f'{self.debug_mode}=log')
        self.assertTrue(guc_res, '执行失败' + text)
        restart_res = self.primary_sh.restart_db_cluster()
        self.assertTrue(restart_res, '执行失败' + text)
        show_msg = self.common.show_param(self.debug_mode)
        self.assertEquals(show_msg, 'log', '执行失败' + text)

    def tearDown(self):
        text = '-----step9:删除用户，恢复参数;expect:删除用户成功，恢复成功-----'
        self.logger.info(text)
        drop_cmd = f"drop user {self.user_sysadmin},{self.user_common};"
        self.logger.info(drop_cmd)
        drop_msg = self.primary_sh.execut_db_sql(drop_cmd)
        self.logger.info(drop_msg)

        debug_mode_current = self.common.show_param(self.debug_mode)
        if debug_mode_current != self.debug_mode_default:
            self.primary_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.debug_mode}={self.debug_mode_default}')
        show_debug_mode = self.common.show_param(self.debug_mode)

        self.logger.info('-----断言tearDown-----')
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG,
                      drop_msg, '执行失败:' + text)
        self.assertEquals(show_debug_mode, self.debug_mode_default,
                          '执行失败' + text)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
